$(function () {
	var obj = {
		money: $('.make_money'),			//已赚现金
		yjhy : $('.first span'),			//一级好友人数
		ejhy : $('.second span')			//二级好友人数
	}
	
	getFridentList();
	function getFridentList(){
		var exist_one = false;		// 判断是否存在一级好友
		var exist_two = false;		// 判断是否存在二级好友
		$.ajax({
			type:'GET',
			url:'http://a.91jfk.com/Product/User/friend_list',
			dataType:'json',
			success:function(res){
				console.log(res);
				if (res.code == 1) {
					var msg = res.list;
					for (var i = 0;i < msg.length;i++) {
						if (msg[i].path.substring(0,1) == '1') {
							exist_one = true;
						} else {
							exist_two = true;
						}
					}
				    $(".first_friends").bind("click",function(){
						if(exist_one){
						    window.location.href = "fridentList.html?id=" + '1';		// 点击一级好友按钮跳转好友页
					    } else {
						    window.location.href = "fridents.html";
					    }
				    });
				    $(".second_fridents").bind("click",function(){
						if(exist_two){
						    window.location.href = "fridentList.html?id=" + '2';		// 点击二级好友按钮跳转好友页
					    } else {
						    window.location.href = "fridents.html";
					    }
				   });
				}
			},
			error:function(err){
				console.log(err);
			}
		});
	}
    
    // 好友数量
    GetFriendsNum();			// 进去页面获取好友数量
    function GetFriendsNum(){
    	$.ajax({
			type:'GET',
			url:'http://a.91jfk.com/Product/User/self_list',
			dataType:'json',
			success:function(res){
				console.log(res);
				if (res.code == '1') {
					var msg = res.list;
					obj.yjhy.text(msg.lv1);		// 一级好友
					obj.ejhy.text(msg.lv2);		// 二级好友
					obj.money.text(msg.cash);	// 已赚佣金
				}
			},
			error:function(err){
				console.log(err);
			}
		});
    }
    
    // 加载排名情况
    loadRanking();
    function loadRanking(){
    	$.ajax({
			type:'GET',
			url:'http://a.91jfk.com/Product/User/billboard',
			dataType:'json',
			success:function(res){
				console.log(res);
				if (res.code == 1) {
					var msg = res.list;
					$('.two_box .ranking').text('第'+msg.self+'名');
					$('.two_box .time').text(CurentTime());
					delete msg.self;
					$.each(msg, function(i,item) {
						loadLi((i-0)+1,item.tel,item.lv1,item.lv2,item.total);
					});
				} 
			},
			error:function(err){
				console.log(err);
			}
		});
	}
	function loadLi(i,tel,one,two,money){
		var ul = $('.Info_ul');
		var li = $('<li class="detail_info"><p><span>'+i+'</span></p><p>'+tel+'</p><p>'+one+'人</p><p>'+two+'人</p><p>'+money+'</p></li>');
		ul.append(li);
	}
    // 点击赚钱攻略按钮跳转赚钱攻略页
    $(".method").bind("click",function(){
    	window.location.href = "manage.html"
    });
    // 点击流动明细按钮跳转流动明细页
    $(".mingxi").bind("click",function(){
    	window.location.href = "flow_details.html"
    });
    // 点击一级好友按钮跳转好友页
    $(".first_friends").bind("click",function(){
    	console.log(11111);
    });
    // 点击二级好友按钮跳转好友页
    $(".second_fridents").bind("click",function(){
    	console.log(22222);
    });
    
    $('.hero_heard img').bind('click',function(){
    	window.location.href = document.referrer;	//返回上一页并刷新
    });
    
    //点击立即邀请
    $(".hero_foot").click(function(){
        $(".mask").css("display","block");
        $(".prompt").css("display","block");
    });
    $(".prompt>p img").click(function(){
        $(".prompt").css("display","none");
        $(".mask").css("display","none");
    });
    function CurentTime(){ 
        var now = new Date();
        var year = now.getFullYear();       //年
        var month = now.getMonth() + 1;     //月
        var day = now.getDate();            //日
        var clock = year + "-";
        if(month < 10){
        	clock += "0";
        }
        clock += month + "-";
        if(day < 10){
        	clock += "0";
        }
        clock += day;
        return(clock); 
    }
});